/*
 * @Author: Salaing
 * @Date: 2024-07-07 13:31:49
 * @LastEditors: Salaing
 */
import { Tag, Image } from 'antd';
import { formatYear, formatDate, formatBirth } from 'utils/format';
import loadErrorImg from 'common/img/load_error.png';
import { mapData } from 'utils/mapData';
import { staffRule } from 'utils/rules/staffRule';
import iconMap from 'components/IconMap';

const Columns = ({ handleSave, userInfo, openReviewRecord, openDetailDialog }) => {
  /* 
    accountName: "letvpn"
    avatar: "https://reactoa.hyfarsight.com/FrBl9IMFBoOS3a6tvN9DnjMSs_cK"
    bankNumber: "123456789123456789"
    education: 1
    graduatedSchool: "天津大学"
    hometown: "北京市朝阳区"
    idNumber: "211302198305112819"
    identity: 0
    marriage: 0
    onboardingTime: "2021-11-12T09:43:26.939Z"
    salary: "4000"
   */
  const normalList = [
    {
      title: '姓名',
      dataIndex: 'userName',
      editable: true,
      render: (userName, { _id }) => {
        return <>
          <span className='userName'>{userName}</span>
          {
            userInfo?.identity === 1 && <span className='c-r' onClick={(e) => {
              e.stopPropagation()
              handleSave(_id)
            }}>
              {iconMap.edit}
            </span>

          }
        </>
      },
    },
    {
      title: '联系电话',
      editable: true,
      dataIndex: 'mobile',
    },
    {
      title: '职级描述',
      dataIndex: 'level',
      render: (data) => data?.levelDescription || '暂无职级描述',
    },
    {
      title: '性别',
      dataIndex: 'gender',
      editable: true,
      render: (type) => <Tag>{mapData.gender[type]}</Tag>,
    },
    {
      title: '部门',
      dataIndex: 'department',
      render: (data) => data?.departmentName || '---',
    },
    {
      title: '部门负责人',
      dataIndex: 'department',
      render: (data) => data?.departmentLeader?.userName || '---',
    },
  ];

  const authList = [
    {
      title: '入职时间',
      dataIndex: 'onboardingTime',
      width: '200px',
      editable: true,
      render: (date) => formatDate(date, 'YYYY-MM-DD'),
    },
    {
      title: '年龄',
      dataIndex: 'idNumber',
      width: '200px',
      render: (idNumber) => formatYear(idNumber, 'age'),
    },
    {
      title: '头像',
      dataIndex: 'avatar',
      editable: true,
      render: (img) => <Image src={img || 'error'} fallback={loadErrorImg} width={50} height={50} />,
    },
    {
      title: '籍贯',
      dataIndex: 'hometown',
      editable: true,
      render: (hometown) => {
        hometown || '--';
      },
    },
    {
      title: '学历',
      dataIndex: 'education',
      editable: true,
      render: (type) => <Tag> {mapData['education'][type]}</Tag>,
    },
    {
      title: '婚姻状况',
      dataIndex: 'marriage',
      editable: true,
      render: (type) => <Tag> {mapData['marriage'][type]}</Tag>,
    },
    {
      title: '生日',
      dataIndex: 'idNumber',
      render: (id) => formatBirth(id),
    },
    {
      title: '银行卡',
      dataIndex: 'bankNumber',
      editable: true,
    },
    {
      title: '身份证号',
      editable: true,
      dataIndex: 'idNumber',
    },
    {
      title: '毕业院校',
      editable: true,
      dataIndex: 'graduatedSchool',
    },
    {
      title: '绩效考核',
      dataIndex: 'record',
      render: (record, data) => {
        return (
          <Tag
            onClick={() =>
              openReviewRecord({
                title: '考核记录',
                interfaceName: 'getAssessmentList',
                requestData: {
                  queryData: { staffName: data._id },
                },
                type: 'assessment',
              })
            }
            className="c-p"
          >
            查看
          </Tag>
        );
      },
    },
    {
      title: '奖惩记录',
      dataIndex: 'record',
      render: (record, data) => {
        return (
          <Tag
            onClick={() =>
              openReviewRecord({
                title: '奖惩记录',
                interfaceName: 'getRewardAndPunishment',
                requestData: {
                  staffName: data._id,
                },
                type: 'reward',
              })
            }
            className="c-p"
          >
            查看
          </Tag>
        );
      },
    },
    {
      title: '调薪记录',
      dataIndex: 'record',
      render: (record, data) => {
        return (
          <Tag
            onClick={() =>
              openReviewRecord({
                title: '调薪记录',
                interfaceName: 'getSalaryAdjustment',
                requestData: {
                  staffName: data._id,
                },
                type: 'salary',
              })
            }
            className="c-p"
          >
            查看
          </Tag>
        );
      },
    },
  ];

  let renderColumnsList =
    userInfo?.identity === 0 ? normalList : [...normalList, ...authList];

  renderColumnsList = renderColumnsList.map((col) => {
    if (!col.editable || userInfo?.identity !== 1) {
      return col; //- 直接返回当前的渲染单元格
    }
    return {
      ...col,
      onCell: (record) => {
        //创建一个规定编辑表单类型的实行type
        let type = '';
        switch (col.dataIndex) {
          case 'onboardingTime':
          case 'idNumber':
            type = 'dateNode'
            break;
          case 'gender':
          case 'education':
          case 'marriage':
            type = 'selectNode'
            break;
          default:
            type = 'inputNode'
            break;
        }
        return {
          record,
          type,
          editable: col.editable,
          dataIndex: col.dataIndex,
          title: col.title,
          rules: staffRule[col.dataIndex],
          handleSave: handleSave,
        }

      },
    };
  });

  return renderColumnsList;
};

export default Columns;
